package com.imwmw.pulldownwidget;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.imwmw.Cheeses;

import java.util.ArrayList;

public class MainActivity extends Activity {

    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView = (ListView) findViewById(R.id.lv);
        mListView.setAdapter(new CustomAdapter(this));

        final View header = LayoutInflater.from(this).inflate(R.layout.layout_listview_header, null);
        Button btnShow = (Button) header.findViewById(R.id.btn_show);
        final TextView tvSlide = (TextView) header.findViewById(R.id.tv_slide);
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("XXX", "onClick");
                tvSlide.setHeight(200);
            }
        });
        mListView.addHeaderView(header);
    }

    static class CustomAdapter extends BaseAdapter {
        private Context mCtx;
        private final ArrayList<String> mData;

        public CustomAdapter(Context c) {
            mData = Cheeses.asList();
            mCtx = c;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new TextView(mCtx);
                int padding = (int) mCtx.getResources().getDimension(R.dimen.padding);
                convertView.setPadding(padding, padding, padding, padding);
            }
            ((TextView) convertView).setText(mData.get(position));
            return convertView;
        }
    }
}
